package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.LogService;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ConfirmOrderServlet extends HttpServlet {
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/confirmOrder.jsp";
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/shippingForm.jsp";

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String cardType = req.getParameter("cardType");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");

        // 修改订单消息
        Order order = (Order)session.getAttribute("order");
        order.setCardType(cardType);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
        order.setBillToFirstName(firstName);
        order.setBillToLastName(lastName);
        order.setShipToFirstName(firstName);
        order.setShipToLastName(lastName);
        order.setBillAddress1(address1);
        order.setBillAddress2(address2);
        order.setShipAddress1(address1);
        order.setShipAddress2(address2);
        order.setBillCity(city);
        order.setShipCity(city);
        order.setBillState(state);
        order.setShipState(state);
        order.setBillZip(zip);
        order.setShipZip(zip);
        order.setBillCountry(country);
        order.setShipCountry(country);

        session.setAttribute("order",order);
        OrderService orderService=new OrderService();
        orderService.insertOrder(order);

        if(req.getParameter("shippingAddressRequired") != null){
            Account account = (Account) session.getAttribute("account");
            if(account != null) {
                String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+  "  " + "跳转至地址更改页面";
                logService.insertLogInfo(account.getUsername(),logInfo);
            }

            req.getRequestDispatcher(SHIPPING_FORM).forward(req,resp);
        }else{
            Account account = (Account) session.getAttribute("account");
            if(account != null) {
                String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ")+"用户" +account.getUsername()+  "  "+"跳转至订单确认页面";
                logService.insertLogInfo(account.getUsername(),logInfo);
            }

            req.getRequestDispatcher(CONFIRM_ORDER).forward(req,resp);
        }

    }
}
